import { reqLogin, reqLogout } from '@/apis/user.js';
// import { reqTotalCount } from '@/apis/rexiaogoods.js';
import { setToken, getToken, getCodeList, setCodeList, getNameList, setNameList, removeStatus, removeToken, removeNameList, removeCodeList } from '@/utils/auth.js';
const state = {
	token: getToken(),
	userInfo: {},
	roleCodeList: getCodeList() || [],
	roleNameList: getNameList() || []
};
const actions = {
	// 登录
	async userLogin({ dispatch, commit }, userInfo) {
		let res = await reqLogin(userInfo);
		if (res.code === 200) {
			// 存储一下token
			console.log(res.data);
			console.log('res登录', res.data);
			commit('USER_LOGIN', res.data.token);
			commit('USER_CODE_LIST', res.data.roleCodeList);
			commit('USER_NAME_LIST', res.data.roleNameList);
			return res;
		}
	},
	// async userInfo({ commit }) {
	//   let res = await reqUserInfo();
	//   if (res.code == 200) {
	//     // 存一下
	//     commit('USER_INFO', res.data);
	//     return 'OK';
	//   }else {
	//     return Promise.reject(new Error(res.message));
	//   }
	// },
	async userLogout(context) {
		let res = await reqLogout();
		if (res.code == 200) {
			// commit('USER_LOGOUT');
			removeToken();
			removeStatus();
			removeNameList();
			removeCodeList();
			return res;
		}
	}
};
const mutations = {
	USER_LOGIN(state, token) {
		state.token = token;
		// 持久化存储
		setToken(token);
	},
	USER_CODE_LIST(state, roleCodeList) {
		state.roleCodeList = roleCodeList;
		setCodeList(roleCodeList);
	},
	USER_NAME_LIST(state, roleNameList) {
		state.roleNameList = roleNameList;
		setNameList(roleNameList);
	}
};
const getters = {
	userInfo(state) {
		return state.userInfo || [];
	}
};
export default {
	namespaced: true,
	actions,
	mutations,
	state,
	getters
};
